package com.link2room.ironhide.controller.manager;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.base.RoomtypeBaseParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelRmtypePictureDto;
import com.link2room.ironhide.dto.result.base.L2RRoomtypeDto;
import com.link2room.ironhide.facade.IRoomtypeFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.link2room.ironhide.constant.PermissionConstant.*;

/**
 * Created by sss on 2016/8/29.
 */
@Api(tags = "房型管理")
@RestController
@RequestMapping("/manager/Roomtypes")
public class RoomtypeManagerController {
    @Resource
    private IRoomtypeFacadeService roomtypeFacadeService;

    @ApiOperation(value="获取房型列表",response = L2RRoomtypeDto.class)
    @RequestMapping(method= RequestMethod.GET)
    @L2RPermissionResources(value = roomtypeList,name="房型列表")
    public @ResponseBody BaseResultDto roomtypeList(
            @RequestParam(value = "lhotelCode" ,required = true) String lhotelCode
    ) throws Exception {
        RoomtypeBaseParamDto roomtypeBaseParamDto =new RoomtypeBaseParamDto();
        roomtypeBaseParamDto.setLhotelCode(lhotelCode);
        QueryResult<L2RRoomtypeDto> roomtypeResult = roomtypeFacadeService.listRoomType(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(roomtypeResult);
        return l2rRs;
    }

    @ApiOperation(value="获取房型详情",response = L2RRoomtypeDto.class)
    @RequestMapping(value = "/{id}",method= RequestMethod.GET)
    @L2RPermissionResources(value = roomtypeInfo,name="房型详情信息")
    public @ResponseBody BaseResultDto hotelBaseInfoList(
            @PathVariable Long id
    ) throws Exception {
        RoomtypeBaseParamDto roomtypeBaseParamDto =new RoomtypeBaseParamDto();
        roomtypeBaseParamDto.setId(id);
        L2RRoomtypeDto roomtype = roomtypeFacadeService.getRoomtypeDtoById(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(roomtype);
        return l2rRs;
    }

    @ApiOperation(value="保存房型",response = L2RRoomtypeDto.class)
    @RequestMapping(method= RequestMethod.POST)
    @L2RPermissionResources(value = newRoomtype,name="新建房型")
    public @ResponseBody BaseResultDto newRoomtype(
            @RequestBody RoomtypeBaseParamDto roomtypeBaseParamDto
    ) throws Exception {
        L2RRoomtypeDto roomtype = roomtypeFacadeService.saveOrUpdateRoomtypeDto(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(roomtype);
        return l2rRs;
    }

    @ApiOperation(value="更新房型",response = L2RRoomtypeDto.class)
    @RequestMapping(method= RequestMethod.PUT)
    @L2RPermissionResources(value = updateRoomtype,name="更新房型")
    public @ResponseBody BaseResultDto updateRoomtype(
            @RequestBody RoomtypeBaseParamDto roomtypeBaseParamDto
    ) throws Exception {
        L2RRoomtypeDto roomtype = roomtypeFacadeService.saveOrUpdateRoomtypeDto(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(roomtype);
        return l2rRs;
    }

    @ApiOperation(value="更新房型基础信息",response = L2RRoomtypeDto.class)
    @RequestMapping(value = "/RoomtypeBases",method= RequestMethod.PUT)
    @L2RPermissionResources(value = updateRoomtypeBase,name="更新房型基础信息")
    public @ResponseBody BaseResultDto updateRoomtypeBase(
            @RequestBody RoomtypeBaseParamDto roomtypeBaseParamDto
    ) throws Exception {
        L2RRoomtypeDto roomtype = roomtypeFacadeService.saveOrUpdateRoomtypeBase(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(roomtype);
        return l2rRs;
    }

    @ApiOperation(value="获取房型大图",response = L2RHotelRmtypePictureDto.class)
    @RequestMapping(value = "/RoomtypeBigPics",method= RequestMethod.GET)
    @L2RPermissionResources(value = roomtypeBigPics,name = "获取房型大图")
    public @ResponseBody BaseResultDto roomtypeBigPics(
            @RequestParam(value = "lhotelCode" ,required = true) String lhotelCode,
            @RequestParam(value = "rmtypes" ,required = false) String rmtypes
    ) throws Exception {
        List<String> rmtypeList=new ArrayList();
        if(StringUtil.isNotBlank(rmtypes)){
            String[] rmtypeArr = rmtypes.split(",");
            for(String rmtype:rmtypeArr){
                rmtypeList.add(rmtype);
            }
        }

        RoomtypeBaseParamDto roomtypeBaseParamDto =new RoomtypeBaseParamDto();
        roomtypeBaseParamDto.setLhotelCode(lhotelCode);
        roomtypeBaseParamDto.setRmtypes(rmtypeList);
        QueryResult<L2RHotelRmtypePictureDto> hotelBigPicResult = roomtypeFacadeService.getRmtypeBigPic(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(hotelBigPicResult);
        return l2rRs;
    }

    @ApiOperation(value="获取房型多图",response = L2RHotelRmtypePictureDto.class)
    @RequestMapping(value = "/RoomtypePics",method= RequestMethod.GET)
    @L2RPermissionResources(value = roomtypePics,name = "获取房型多图")
    public @ResponseBody BaseResultDto roomtypePics(
            @RequestParam(value = "lhotelCode" ,required = true) String lhotelCode,
            @RequestParam(value = "rmtypes" ,required = false) String rmtypes
    ) throws Exception {
        List<String> rmtypeList=new ArrayList();
        if(StringUtil.isNotBlank(rmtypes)){
            String[] rmtypeArr = rmtypes.split(",");
            for(String rmtype:rmtypeArr){
                rmtypeList.add(rmtype);
            }
        }
        RoomtypeBaseParamDto roomtypeBaseParamDto =new RoomtypeBaseParamDto();
        roomtypeBaseParamDto.setLhotelCode(lhotelCode);
        roomtypeBaseParamDto.setRmtypes(rmtypeList);
        QueryResult<L2RHotelRmtypePictureDto> hotelBigPicResult = roomtypeFacadeService.getRmtypePics(roomtypeBaseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(hotelBigPicResult);
        return l2rRs;
    }
}
